<?php
namespace Huangzikun\hawkeye_spider;

class SpiderConfigClass
{
    private $getConfigUri = '/api/v1/get/machine/spiderconfig/';
    public function getSpiderConfig()
    {
        $path = __DIR__ . "/../config/config.json";
        $config = file_get_contents($path) or die("can't find config!!!");
        $config = json_decode($config, true);

        $url =  "http://{$config['remote_host_ip']}:{$config['remote_host_port']}{$this->getConfigUri}{$config['machine_id']}";

        $context = stream_context_create(array(
            'http' => array(
                'timeout' => 3 //超时时间，单位为秒
            )
        ));
        $newConfig = file_get_contents($url, false, $context);
        $newConfig = json_decode($newConfig, true);
        if(empty($newConfig) || $newConfig['code']!=1){
            return ;
        }

        $config['collection'] = $newConfig['msg'];
        $config = json_encode($config);
        file_put_contents($path, $config);
    }
}